#include"friendmodel.hpp"
#include"db.hpp"
void Friendmodel::insert(int userid,int friendid)
{
        // 1.组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "insert into friend values(%d, %d)", userid, friendid);

    Mysql mysql;
    if (mysql.connect())
    {
        mysql.update(sql);
    }
}


vector<User> Friendmodel::query(int userid)
{
    char sql[1024]={0};
    sprintf(sql,"select a.id,a.name,a.state from user a inner join friend b on b.friendid = a.id where b.userid=%d",userid);
    vector<User> vec;
    Mysql mysql;
    if(mysql.connect())
    {
        MYSQL_RES* res=mysql.query(sql);
        if(res!=nullptr)
        {
            //读取数据
            MYSQL_ROW row;
            while ((row=mysql_fetch_row(res))!=nullptr)
            {
                User user;
                user.setid(atoi(row[0]));
                user.setname(row[1]);
                user.setstate(row[2]);
                vec.push_back(user);
            }
            mysql_free_result(res);
        }
    }
    return vec;
}